
#include "mbm_mstar_inc.h"

#include "mbm_typedefine.h"
#include "mbm_jpeg_dev_adapter.h"


uint32 mbm_ext_mem_left_size(void);


#define MY_W 160
#define MY_H 120

#define MY_W2 80
#define MY_H2 64

unsigned int capture_buf_addr = NULL;
unsigned int review_buf_addr = NULL;


void mbm_camera_test(void)
{
    int my_count;
    t_mbm_camera_capture_param param;
    t_yuv_data ispdata;
    
    DCAMERA_YUV_CHN_ADDR_T yuv;
    uint16 w, h;
    uint32 j_buf;
    uint32 j_size;
    uint32 d_buf;

    capture_buf_addr = SCI_ALLOC_APP(MY_W*MY_H*2+256);
    review_buf_addr = SCI_ALLOC_APP(MY_W*MY_H*2+256);
    j_buf = (uint8*)SCI_ALLOC_APP(MY_W*MY_H*2+256);
    d_buf = SCI_ALLOC_APP(MY_W*MY_H*2+256);

    capture_buf_addr = DC_RIGHT_TRUNK(capture_buf_addr, 256); 
    review_buf_addr = DC_RIGHT_TRUNK(review_buf_addr, 256); 
    j_buf = DC_RIGHT_TRUNK(j_buf, 256); 
    d_buf = DC_RIGHT_TRUNK(d_buf, 256); 
    mbm_trace("j 0x%x, d 0x%x", j_buf, d_buf);

    param.capture_width = MY_W;
    param.capture_height = MY_H;
    param.capture_buf = DC_RIGHT_TRUNK(capture_buf_addr, 256); ; 
    param.capture_len = param.capture_width*param.capture_height*2;

    mbm_camera_open();
    my_count = 0;

    mbm_trace("capture 0x%x, review 0x%x, left %d", capture_buf_addr, review_buf_addr, mbm_ext_mem_left_size());
    while(my_count++ < 100)
    {
        mbm_camera_capture(&ispdata, &param);
        mbm_trace("capture y:0x%x U0x%x", ispdata.y_addr, ispdata.uv_addr);

        mbm_EncodeJpgBuf(ispdata.y_addr, ispdata.uv_addr, MY_W, MY_H, &w, &h, j_buf, &j_size);
        mbm_trace("mbm_EncodeJpgBuf 0x%x, 0x%x [%dx%d] buf0x%x size0x%d", j_buf, j_size, w, h, j_buf, j_size);

        mbm_DecodeJpgBufYUV(j_buf, j_size, d_buf, &yuv.y_chn_addr, &yuv.u_chn_addr, &w, &h);
        mbm_trace("mbm_DecodeJpgBuf2 0x%x, 0x%x, [%dx%d]", yuv.y_chn_addr, yuv.u_chn_addr, w, h);

        //yuv.y_chn_addr = ispdata.y_addr;
        //yuv.u_chn_addr = ispdata.uv_addr;
        my_dispaly_yuv2(&yuv);
    }

    //fixme
    SCI_Free(capture_buf_addr);
    SCI_Free(review_buf_addr);

    mbm_camera_close( );
}

